/*
 * @Author: szx
 * @Date: 2022-02-18 12:57:16
 * @LastEditTime: 2022-02-18 13:17:54
 * @Description:
 * @FilePath: \leetcode\1-99\59\59_2.js
 */
/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
    let x = 0,
        y = 0;
    const res = new Array(n).fill(0).map(() => new Array(n).fill(0));
    let cnt = 1;
    const mid = Math.floor(n / 2);
    for (let loop = 1; loop <= mid; loop += 1) {
        let row = x,
            col = y;
        for (; col < n - loop; col++) res[row][col] = cnt++;
        for (; row < n - loop; row++) res[row][col] = cnt++;
        for (; col > y; col--) res[row][col] = cnt++;
        for (; row > x; row--) res[row][col] = cnt++;
        x++;
        y++;
    }
    if (n & 1) res[mid][mid] = cnt;
    return res;
};

console.table(generateMatrix(1));
